/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.ufsc.aps.jogodavida.view;

import br.ufsc.aps.jogodavida.model.Carta;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.List;

/**
 *
 * @author gustavo
 */
public class DialogCartaNegativa extends javax.swing.JDialog {
    
    private Carta cartaNegativa;

    /**
     * Creates new form DialogCartaNegativa
     */
    public DialogCartaNegativa(PanelTabuleiro parent, boolean modal, List<Carta> cartas, Carta cartaNegativa) {
        super(parent.getParentFrame(), modal);
        this.cartaNegativa = cartaNegativa;
        initComponents();
        this.mostrarCartas(cartas, parent);
        this.setLocationRelativeTo(parent);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        panelBase = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(panelBase, javax.swing.GroupLayout.DEFAULT_SIZE, 600, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(panelBase, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * @param args the command line arguments
     */
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel panelBase;
    // End of variables declaration//GEN-END:variables

    private void mostrarCartas(List<Carta> cartas, PanelTabuleiro parent) {
        for (Carta c : cartas) {
            if(c.getPontuacao() >= (-cartaNegativa.getPontuacao())) {
                LabelCarta lc = new LabelCarta(c,true);
                lc.addMouseListener(createMouseListener(parent, lc));
                panelBase.add(lc);
            }
        }
    }
    
    private MouseListener createMouseListener(final PanelTabuleiro parent,final LabelCarta lc) {
        return new MouseAdapter() {
            
            @Override
            public void mouseClicked(MouseEvent e) {
                parent.pagarCartaNegativa(lc.getCarta(),cartaNegativa);
                dispose();
            }
            
        };
    }
}
